#pragma once

#include <ctemplate/template.h>

#include "oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View()
        {
        }
        void AllExpandHtml(const std::vector<Question> &questions, std::string *html)
        {
            // 题目编号  标题  难度
            // 推荐使用表格显示

            // 形成路径
            std::string src_html = template_path + "all_questions.html";

            // 形成template字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                // 形成子字典
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }
            // 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 开始渲染
            tpl->Expand(html, &root);
        }
        void OneExpandHtml(const Question &question, std::string *html)
        {
            // 形成路径
            std::string src_html = template_path + "one_question.html";

            //形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",question.number);
            root.SetValue("desc",question.desc);
            root.SetValue("title",question.title);
            root.SetValue("star",question.star);
            root.SetValue("pre_code",question.header);
            
            // 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            tpl->Expand(html,&root);
        }
        ~View()
        {
        }
    };
}